package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.VideoUploadCallbackHistory;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by haojiayao on 16-3-29.
 */
public class VideoUploadCallbackHistoryConvert {

    public static DBObject castCallbackHistoryToDBObject(VideoUploadCallbackHistory callbackHistory) {

        DBObject dbo = new BasicDBObject();

        dbo.put("outKey", ObjUtils.toLong(callbackHistory.getOutKey()));
        if (callbackHistory.getMid() != null) {

            dbo.put("mid", ObjUtils.toLong(callbackHistory.getMid()));
        }
        if (callbackHistory.getVid() != null) {

            dbo.put("vid", ObjUtils.toLong(callbackHistory.getVid()));
        }
        dbo.put("vType", ObjUtils.toInteger(callbackHistory.getVType()));
        if (callbackHistory.getCodeId() != null) {

            dbo.put("codeId", ObjUtils.toInteger(callbackHistory.getCodeId()));
        }
        dbo.put("size", ObjUtils.toLong(callbackHistory.getSize(), 0L));
        dbo.put("duration", ObjUtils.toLong(callbackHistory.getDuration(), 0L));
        dbo.put("curTask", ObjUtils.toString(callbackHistory.getCurTask()));
        dbo.put("code", ObjUtils.toString(callbackHistory.getCode()));
        dbo.put("msg", ObjUtils.toString(callbackHistory.getMsg(), ""));
        if (callbackHistory.getStatus() != null) {

            dbo.put("status", ObjUtils.toInteger(callbackHistory.getStatus()));
        }
        if (callbackHistory.getErrorCode() != null) {

            dbo.put("errorCode", ObjUtils.toInteger(callbackHistory.getErrorCode()));
        }
        if (callbackHistory.getSrcCheckCode() != null) {

            dbo.put("srcCheckCode", ObjUtils.toInteger(callbackHistory.getSrcCheckCode()));
        }
        dbo.put("srcCheckMsg", ObjUtils.toString(callbackHistory.getSrcCheckMsg(), ""));
        dbo.put("createTime", System.currentTimeMillis());

        return dbo;
    }

    public static VideoUploadCallbackHistory castDBObjectToCallbackHistory(DBObject dbObject) {

        if (dbObject != null) {

            VideoUploadCallbackHistory callbackHistory = new VideoUploadCallbackHistory();

            callbackHistory.setId(ObjUtils.toLong(dbObject.get("_id")));
            callbackHistory.setOutKey(ObjUtils.toLong(dbObject.get("outKey")));
            callbackHistory.setMid(ObjUtils.toLong(dbObject.get("mid")));
            callbackHistory.setVid(ObjUtils.toLong(dbObject.get("vid")));
            callbackHistory.setVType(ObjUtils.toInteger(dbObject.get("vType")));
            callbackHistory.setCodeId(ObjUtils.toInteger(dbObject.get("codeId")));
            callbackHistory.setSize(ObjUtils.toLong(dbObject.get("size")));
            callbackHistory.setDuration(ObjUtils.toLong(dbObject.get("duration")));
            callbackHistory.setCurTask(ObjUtils.toString(dbObject.get("curTask")));
            callbackHistory.setCode(ObjUtils.toString(dbObject.get("code")));
            callbackHistory.setMsg(ObjUtils.toString(dbObject.get("msg"), ""));
            callbackHistory.setStatus(ObjUtils.toInteger(dbObject.get("status")));
            callbackHistory.setErrorCode(ObjUtils.toInteger(dbObject.get("errorCode")));
            callbackHistory.setSrcCheckCode(ObjUtils.toInteger(dbObject.get("srcCheckCode")));
            callbackHistory.setSrcCheckMsg(ObjUtils.toString(dbObject.get("srcCheckMsg")));
            callbackHistory.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));

            return callbackHistory;
        } else {
            return null;
        }
    }
}
